﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DAL;
using BIZ;

namespace SinhNhat.admin
{
    public partial class AdminServiceOrder : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                InitData();
                LoadData();
            }
        }

        private void InitData()
        {
            WebUtils.FormatDate();

            ddlStatus.Items.Add("1...Tất cả");
            ddlStatus.Items.Add("2...Đã xử lý");
            ddlStatus.Items.Add("3...Chưa xử lý");

            DateTime now = DateTime.Now;
            hdFromDate.Value = txtFromDate.Text = string.Format("01/{0:MM/yyyy}", now);
            DateTime to = now.AddMonths(1);
            to = to.AddDays(to.Day * -1);
            hdToDate.Value = txtToDate.Text = to.ToShortDateString();
        }

        private void LoadData()
        {
            WebUtils.FormatDate();

            ServiceOrderBIZ orderBIZ = new ServiceOrderBIZ();
            List<SERVICEORDER> listOrder = orderBIZ.GetAll(ddlStatus.SelectedIndex, DateTime.Parse(hdFromDate.Value), DateTime.Parse(hdToDate.Value));

            if (listOrder != null && listOrder.Count > 0)
            {
                grOrder.DataSource = listOrder;
                grOrder.DataBind();
                lblHeader.Text = "Danh sách đơn đặt dịch vụ";
            }
            else
            {
                grOrder.DataSource = null;
                grOrder.DataBind();

                lblHeader.Text = "Không có đơn đặt dịch vụ nào";
            }

            ClientScript.RegisterStartupScript(this.GetType(), "exampleScript", "<script language ='javascript'>AfterSearch();</script>");
        }

        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
            grOrder.RowCommand += new GridViewCommandEventHandler(grOrder_RowCommand);
            grOrder.PageIndexChanging += new GridViewPageEventHandler(grOrder_PageIndexChanging);
            btnSearch.Click += new EventHandler(btnSearch_Click);
            ddlStatus.SelectedIndexChanged += new EventHandler(ddlStatus_SelectedIndexChanged);
        }

        void grOrder_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            grOrder.PageIndex = e.NewPageIndex;
            grOrder.DataBind();
        }

        void ddlStatus_SelectedIndexChanged(object sender, EventArgs e)
        {
            LoadData();
        }

        void btnSearch_Click(object sender, EventArgs e)
        {
            LoadData();
        }

        void grOrder_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            Guid orderID = WebUtils.ToGuid(e.CommandName);
            ServiceOrderBIZ orderBIZ = new ServiceOrderBIZ();

            if (e.CommandArgument.Equals("True"))
            {
                orderBIZ.ChangeStatus(orderID, false);
            }
            else if (e.CommandArgument.Equals("False"))
            {
                orderBIZ.ChangeStatus(orderID, true);
            }

            LoadData();
        }
    }
}